﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Util.Concurrent
{
    public class LockSupport
    {
        private static LocalDataStoreSlot slot = Thread.GetNamedDataSlot("LockSupport.Park");

        public static void Park(Object blocker)
        {
            Thread thread = Thread.CurrentThread;
            Thread.SetData(slot, blocker);
            lock (thread)
            {
                Monitor.Wait(thread);
            }
        }

        public static void Unpark(Thread thread)
        {
            if (thread == null) return;

            lock (thread)
            {
                Monitor.Pulse(thread);
            }
        }
    }
}
